<?php

// +----------------------------------------------------------------------
// | 随手拍
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;

class SuipaiController extends AdminBase {

    //商家列表
    public function index(){
        if (IS_POST) {
            $this->redirect('index', $_POST);
        }
        //查询条件
        $where = array();
        $where['status']=true;
        //搜索
        $search = I('get.search');
        if (!empty($search)) {
            $this->assign("search", $search);
            //添加开始时间
            $start_time = I('get.start_time');
            if (!empty($start_time)) {
                $start_time = strtotime($start_time);
                $where['create_time'] = array("EGT", $start_time);
                $this->assign('start_time', $start_time);
            }
            //添加结束时间
            $end_time = I('get.end_time');
            if (!empty($end_time)) {
                $end_time = strtotime($end_time);
                $where['create_time'] = array("ELT", $end_time);
                $this->assign('end_time', $end_time);
            }
            if ($end_time > 0 && $start_time > 0) {
                $where['create_time'] = array(array('EGT', $start_time), array('ELT', $end_time));
            }
            //搜索关键字
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("keyword", $keyword);
                $where['title'] = array("LIKE", "%{$keyword}%");
            }
            //状态
            $status = I('get.status', 0, 'intval');
            /*if ($status==1) {
                $where['status'] = array("EQ", true);
            }elseif($status==2){
                $where['status']=array('EQ',false);
            }*/
            $where['status'] = array("EQ", $status);
            $this->assign('status',$status);
        }
        $model=M('Suipai');
        //信息总数
        $count = $model->where($where)->count();
        $page = $this->page($count, 30);
        $data = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id"=>"DESC"))->select();
        $this->assign('Page', $page->show())
            ->assign('count', $count)
            ->assign('data', $data);
        $this->display();
    }

    /**
     * 编辑作品
     */
    public function edit(){
        if (IS_POST) {
            $post = I('post.');
        	$post['image'] = $post['all_pic'][0];
        	if($post['all_pic'])
        	{
        		$arr = array();
        		foreach($post['all_pic'] as $k => $v)
        		{
        			if($k == 0 || !empty($v))
        			{
        				$arr[] = $v;
        			}
        		}
        		$post['all_pic'] = json_encode($arr);
        	}
        	if($post['status'] != 0)
        	{
        		$post['modify_time'] = time();
        	}
            $data=$post;
            $today=date('Y年m月d日',time());

            $item=M('Suipai')->field('userid,status,title,modify_time')->where(array('id'=>$data['id']))->find();
            $status=M('Suipai')->where(array('id'=>$data['id']))->save($data);
            if ($status!==false) {
                //审核更新用户积分
                if($data['status']==true && $item['status']!==true){
                	if(empty($item['modify_time']))
                	{
                    	$savearr['spnum'] = array('exp','spnum+1');
                    	//$savearr['upoint'] = array('exp','upoint+3');

                    	insertSysmsg($item['userid'],'恭喜作品通过审核','恭喜您的作品《'.$data['title'].'》于'.$today.'通过审核，继续努力哦！详情猛戳 <a href="http://www.hongbeiq.com">www.hongbeiq.com</a>');

                        $where['userid']= array('EQ',$item['userid']);
                        $where['infoid']=array('EQ',$data['id']);
                        $where['action']= array('EQ','spadd');
                        $signnum=getCount('UserDynamic',$where);

                        if($signnum == 0){
                            service('Passport')->userDynamic($item['userid'],3, $item['title'],1,'spadd',$data['id']);
                        }
                    	
                	}
                    //更新随手拍活动作品状态
                    M('ToppicSuipai')->where(array('workid'=>$data['id']))->save(array('status'=>true));
                    //   service('Passport')->userDynamic($item['userid'],3,$item['title'].'通过审核获得3个积分',1,'suipaipass');
                }elseif($item['status']== true && $data['status']!=true){
                    $savearr['spnum'] = array('exp','spnum-1');
                    $savearr['upoint'] = array('exp','upoint-3');
                    //更新随手拍活动作品状态
                    M('ToppicSuipai')->where(array('workid'=>$data['id']))->save(array('status'=>false));
					M('Suipai')->where(array('id' => $data['id']))->save(array('modify_time' => 0));
                    //service('Passport')->userDynamic($item['userid'],3,$item['title'].'不通过审核扣除3个积分',1,'suipaipass',$data['id']);
                }
                M('User')->where(array('userid'=>$item['userid']))->save($savearr);

                $this->success("作品更新成功！");
            } else {
                $this->error('更新失败！');
            }

        } else {
            $id=I('get.id');
            if (empty($id)) {
                $this->error('该作品不存在');
            }
            $data=M('Suipai')->find($id);
            if(!empty($data['all_pic']))
            {
            	$pic = json_decode($data['all_pic'], true);
            	$arr = array();
            	foreach($pic as $k => $v)
            	{
            		$arr[$k]['thumb'] = $v;
            		$temp = explode("/", $v);
            		$temp[4] = str_replace("yf_", "", $temp[4]);
            		$arr[$k]['pic'] = implode("/", $temp);
            	}
            	$data['all_pic'] = $arr;
            }
            else
            {
            	$data['all_pic'][0]['thumb'] = $data['image'];
            	$data['all_pic'][0]['pic'] = $data['image'];
            }
            $this->assign('info',$data);
            $this->display();

        }
    }


    //批量处理信息(审核,取消审核,删除)
    public function batch() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
                case 'nopass':
                    $data=array('status'=>2);
                    $msg='不通过审核';
                    break;
            }
            foreach ($ids as $id) {
                $item=M('Suipai')->field('userid,status,title,modify_time')->where(array('id'=>$id))->find();

                M('Suipai')->where(array('id'=>$id))->save($data);
                //审核更新用户积分
                if($doAct=='checkpass' && $item['status']!=true){
                	if(empty($item['modify_time']))
                	{ 
                    	$savearr['spnum'] = array('exp','spnum+1');
                    	
                        $where['userid']= array('EQ',$item['userid']);
                        $where['infoid']=array('EQ',$id);
                        $where['action']= array('EQ','spadd');
                        $signnum=getCount('UserDynamic',$where);

                        if($signnum == 0){
                            service('Passport')->userDynamic($item['userid'],3, $item['title'],1,'spadd',$id);
                        }

                    	
                	}
                    //更新随手拍活动作品状态
                    M('ToppicSuipai')->where(array('workid'=>$id))->data(array('status'=>true))->save();

                }elseif($doAct!='checkpass' && $item['status']==true){
                    $savearr['spnum'] = array('exp','spnum-1');
                    $savearr['upoint'] = array('exp','upoint-3');

                    M('ToppicSuipai')->where(array('workid'=>$id))->save(array('status'=>false));

                   // M('UserDynamic')->where(array('infoid'=>$id,'userid'=>$item['userid'],'action'=>'spadd'))->setField(array('status'=>false));
                }
                M('User')->where(array('userid'=>$item['userid']))->save($savearr);

            }
            $this->success($msg.'成功！');
        }
    }


    //批量删除
    public function batch_delete(){
        $ids = $_POST['ids'];
        if (!$ids) {
            $this->error('没有信息被选中！');
        }
        $today=date('Y年m月d日',time());

        foreach($ids as $id){
            $item=M('Suipai')->field('userid,status,title')->where(array('id'=>$id))->find();

            M('Suipai')->delete($id);
            //删除更新用户积分与随拍数量
            M('ToppicSuipai')->where(array('workid'=>$id))->delete();
            $savearr['spnum'] = array('exp','spnum-1');
            $savearr['upoint'] = array('exp','upoint-6');
            M('User')->where(array('userid'=>$item['userid']))->save($savearr);
            service('Passport')->userDynamic($item['userid'],6,"随手拍作品违规了扣除6个积分",0,'spadd');

            insertSysmsg($item['userid'],'您的作品被删除','很遗憾您的作品《'.$item['title'].'》于'.$today.'日因违反烘焙圈相关规则被删除！详情请查阅烘焙圈相关协议。');
        }
        $this->success('成功删除信息');
    }

}
